// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Serverless.Fluent;
using Elastic.Clients.Elasticsearch.Serverless.Serialization;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Serverless.MachineLearning;

internal sealed partial class DatafeedConverter : JsonConverter<Datafeed>
{
	public override Datafeed Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		if (reader.TokenType != JsonTokenType.StartObject)
			throw new JsonException("Unexpected JSON detected.");
		IReadOnlyDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Aggregations.Aggregation>? aggregations = default;
		Elastic.Clients.Elasticsearch.Serverless.MachineLearning.DatafeedAuthorization? authorization = default;
		Elastic.Clients.Elasticsearch.Serverless.MachineLearning.ChunkingConfig? chunkingConfig = default;
		string datafeedId = default;
		Elastic.Clients.Elasticsearch.Serverless.MachineLearning.DelayedDataCheckConfig delayedDataCheckConfig = default;
		Elastic.Clients.Elasticsearch.Serverless.Duration? frequency = default;
		IReadOnlyCollection<string>? indexes = default;
		IReadOnlyCollection<string> indices = default;
		Elastic.Clients.Elasticsearch.Serverless.IndicesOptions? indicesOptions = default;
		string jobId = default;
		int? maxEmptySearches = default;
		Elastic.Clients.Elasticsearch.Serverless.QueryDsl.Query query = default;
		Elastic.Clients.Elasticsearch.Serverless.Duration? queryDelay = default;
		IReadOnlyDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Mapping.RuntimeField>? runtimeMappings = default;
		IReadOnlyDictionary<string, Elastic.Clients.Elasticsearch.Serverless.ScriptField>? scriptFields = default;
		int? scrollSize = default;
		while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
		{
			if (reader.TokenType == JsonTokenType.PropertyName)
			{
				var property = reader.GetString();
				if (property == "aggregations" || property == "aggs")
				{
					aggregations = JsonSerializer.Deserialize<IReadOnlyDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Aggregations.Aggregation>?>(ref reader, options);
					continue;
				}

				if (property == "authorization")
				{
					authorization = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.DatafeedAuthorization?>(ref reader, options);
					continue;
				}

				if (property == "chunking_config")
				{
					chunkingConfig = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.ChunkingConfig?>(ref reader, options);
					continue;
				}

				if (property == "datafeed_id")
				{
					datafeedId = JsonSerializer.Deserialize<string>(ref reader, options);
					continue;
				}

				if (property == "delayed_data_check_config")
				{
					delayedDataCheckConfig = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.DelayedDataCheckConfig>(ref reader, options);
					continue;
				}

				if (property == "frequency")
				{
					frequency = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Duration?>(ref reader, options);
					continue;
				}

				if (property == "indexes")
				{
					indexes = JsonSerializer.Deserialize<IReadOnlyCollection<string>?>(ref reader, options);
					continue;
				}

				if (property == "indices")
				{
					indices = JsonSerializer.Deserialize<IReadOnlyCollection<string>>(ref reader, options);
					continue;
				}

				if (property == "indices_options")
				{
					indicesOptions = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.IndicesOptions?>(ref reader, options);
					continue;
				}

				if (property == "job_id")
				{
					jobId = JsonSerializer.Deserialize<string>(ref reader, options);
					continue;
				}

				if (property == "max_empty_searches")
				{
					maxEmptySearches = JsonSerializer.Deserialize<int?>(ref reader, options);
					continue;
				}

				if (property == "query")
				{
					query = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.Query>(ref reader, options);
					continue;
				}

				if (property == "query_delay")
				{
					queryDelay = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Duration?>(ref reader, options);
					continue;
				}

				if (property == "runtime_mappings")
				{
					runtimeMappings = JsonSerializer.Deserialize<IReadOnlyDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Mapping.RuntimeField>?>(ref reader, options);
					continue;
				}

				if (property == "script_fields")
				{
					scriptFields = JsonSerializer.Deserialize<IReadOnlyDictionary<string, Elastic.Clients.Elasticsearch.Serverless.ScriptField>?>(ref reader, options);
					continue;
				}

				if (property == "scroll_size")
				{
					scrollSize = JsonSerializer.Deserialize<int?>(ref reader, options);
					continue;
				}
			}
		}

		return new Datafeed { Aggregations = aggregations, Authorization = authorization, ChunkingConfig = chunkingConfig, DatafeedId = datafeedId, DelayedDataCheckConfig = delayedDataCheckConfig, Frequency = frequency, Indexes = indexes, Indices = indices, IndicesOptions = indicesOptions, JobId = jobId, MaxEmptySearches = maxEmptySearches, Query = query, QueryDelay = queryDelay, RuntimeMappings = runtimeMappings, ScriptFields = scriptFields, ScrollSize = scrollSize };
	}

	public override void Write(Utf8JsonWriter writer, Datafeed value, JsonSerializerOptions options)
	{
		throw new NotImplementedException("'Datafeed' is a readonly type, used only on responses and does not support being written to JSON.");
	}
}

[JsonConverter(typeof(DatafeedConverter))]
public sealed partial class Datafeed
{
	public IReadOnlyDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Aggregations.Aggregation>? Aggregations { get; init; }

	/// <summary>
	/// <para>
	/// The security privileges that the datafeed uses to run its queries. If Elastic Stack security features were disabled at the time of the most recent update to the datafeed, this property is omitted.
	/// </para>
	/// </summary>
	public Elastic.Clients.Elasticsearch.Serverless.MachineLearning.DatafeedAuthorization? Authorization { get; init; }
	public Elastic.Clients.Elasticsearch.Serverless.MachineLearning.ChunkingConfig? ChunkingConfig { get; init; }
	public string DatafeedId { get; init; }
	public Elastic.Clients.Elasticsearch.Serverless.MachineLearning.DelayedDataCheckConfig DelayedDataCheckConfig { get; init; }
	public Elastic.Clients.Elasticsearch.Serverless.Duration? Frequency { get; init; }
	public IReadOnlyCollection<string>? Indexes { get; init; }
	public IReadOnlyCollection<string> Indices { get; init; }
	public Elastic.Clients.Elasticsearch.Serverless.IndicesOptions? IndicesOptions { get; init; }
	public string JobId { get; init; }
	public int? MaxEmptySearches { get; init; }
	public Elastic.Clients.Elasticsearch.Serverless.QueryDsl.Query Query { get; init; }
	public Elastic.Clients.Elasticsearch.Serverless.Duration? QueryDelay { get; init; }
	public IReadOnlyDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Mapping.RuntimeField>? RuntimeMappings { get; init; }
	public IReadOnlyDictionary<string, Elastic.Clients.Elasticsearch.Serverless.ScriptField>? ScriptFields { get; init; }
	public int? ScrollSize { get; init; }
}